<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="年度" prop="year">
        <el-date-picker format="yyyy" value-format="yyyy" v-model="queryParams.year" type="year" placeholder="选择年">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="项目名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入项目名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="开始时间" prop="time">
        <el-date-picker v-model="time" type="daterange" align="right" unlink-panels range-separator="至"
          value-format="yyyy-MM-dd" start-placeholder="开始日期" end-placeholder="结束日期" :picker-options="pickerOptions2"
          @change="changeTime">
        </el-date-picker>
      </el-form-item> -->

      <el-form-item label="项目联系人" prop="projectLinkName">
        <el-input v-model="queryParams.projectLinkName" placeholder="请输入项目联系人" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="是否签署" prop="signState">
        <el-select v-model="queryParams.signState" placeholder="全部" clearable @change="selectMoneyType">
          <el-option label="待签" :value="0" />
          <el-option label="已签" :value="1" />
        </el-select>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-select v-model="queryParams.remark" placeholder="请输入备注" clearable :style="{ width: '100%' }">
          <el-option v-for="dict in dict.type.sys_contract_remark" :key="dict.value" :label="dict.label"
            :value="dict.label" /></el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['project:contract:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['project:contract:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['project:contract:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['project:contract:export']">导出</el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table @selection-change="handleSelectionChange" border fit v-loading="loading" :data="dataList">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" />
      <el-table-column label="项目名称" prop="name" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <el-link type="primary" @click="handleDetail(scope.row)">{{
            scope.row.name
          }}</el-link>
        </template>
      </el-table-column>

      <el-table-column prop="code" show-overflow-tooltip label="合同编号" align="center" min-width="120">
      </el-table-column>
      <el-table-column prop="startDate" show-overflow-tooltip label="开始日期" align="center" width="100">
      </el-table-column>
      <el-table-column prop="endDate" show-overflow-tooltip label="结束日期" align="center" width="100">
      </el-table-column>
      <el-table-column prop="projectLeaderName" show-overflow-tooltip label="项目负责人" align="center" width="100">
      </el-table-column>
      <el-table-column prop="projectLinkName" show-overflow-tooltip label="项目联系人" align="center" width="100">
      </el-table-column>
      <el-table-column prop="totalMoney" show-overflow-tooltip label="合同总金额" align="center" width="100">
      </el-table-column>
      <el-table-column prop="pay" show-overflow-tooltip label="项目总支出" align="center" width="100">
      </el-table-column>
      <el-table-column prop="leaveMoney" show-overflow-tooltip label="剩余款项" align="center" width="100">
      </el-table-column>
      <el-table-column prop="projectRate" show-overflow-tooltip label="项目进度" align="center" width="100">
        <template slot-scope="scope">
          {{ scope.row.projectRate ? scope.row.projectRate : 0 }}%
        </template>
      </el-table-column>
      <el-table-column prop="payRate" show-overflow-tooltip label="到款比例" align="center" width="100">
        <template slot-scope="scope">
          {{ scope.row.payRate ? scope.row.payRate : 0 }}%
        </template>
      </el-table-column>
      <el-table-column prop="projectRate" show-overflow-tooltip label="是否签署" align="center" width="100">
        <template slot-scope="scope">
          {{ scope.row.signState == 0 ? '待签' : '已签' }}
        </template>
      </el-table-column>
      <el-table-column prop="remark" show-overflow-tooltip label="备注" align="center" width="100">
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" v-hasPermi="['project:contract:info']"
            @click="handleDetail(scope.row)">详情</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['project:contract:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['project:contract:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
  </div>
</template>

<script>
import {
  projectContractList,
  projectContractDel,
} from "@/api/business/projectContract.js";
import { userSelect, deptTreeSelect } from "@/api/system/user.js";
export default {
  name: "project",
  dicts: ["project_contract_type","sys_contract_remark"],

  data() {
    return {
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      dataList: [],
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        startTime: '',
        endTime: '',
        year: '',
      },
      time: '',
    };
  },
  created() {
    const str = sessionStorage.getItem('queryParams');
    if (str) {
      this.queryParams = JSON.parse(str);
      setTimeout(() => {
        sessionStorage.removeItem('queryParams');
      }, 500)
    }
    this.getList();
  },
  methods: {
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.projectContractId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
      this.queryParams.ids = this.ids.toString()
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      sessionStorage.setItem('queryParams', JSON.stringify(this.queryParams));
      const configId = row.projectContractId || this.ids;
      this.$router.push("./addContract?isEdit=true&id=" + configId);
    },
    handleDetail(row) {
      sessionStorage.setItem('queryParams', JSON.stringify(this.queryParams));
      this.$router.push(
        "./addContract?isInfo=true&id=" + row.projectContractId
      );
    },
    // 
    changeTime(e) {
      console.log(e);
      this.queryParams.startTime = e[0]
      this.queryParams.endTime = e[1]

    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const configId = row.projectContractId || this.ids;
      let name = this.checkRow(
        configId,
        this.dataList,
        "projectContractId",
        "DELETE",
        "name"
      );
      this.$modal
        .confirm('是否确认删除项目名称为"' + name + '"的合同吗？')
        .then(function () {
          return projectContractDel(configId);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
    handleAdd() {
      this.$router.push("./addContract");
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      projectContractList(this.queryParams).then((response) => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询部门下拉树结构 */
    getDeptTree() {
      deptTreeSelect().then((response) => {
        this.deptOptions = response.data;
      });
    },
    handleExport() {
      this.download(
        "system/projectContract/export",
        {
          ...this.queryParams,
        },
        `合同记录表_${this.parseTime(new Date().getTime(), "{y}-{m}-{d}")}.xlsx`
      );
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.startTime = undefined
      this.queryParams.endTime = undefined
      this.time = undefined
      this.resetForm("queryForm");
      this.handleQuery();
    },
  },
};
</script>
<style lang="scss" scoped>
::v-deep .el-dialog__body {
  img {
    width: 100%;
  }
}
</style>
